431B - Shower Line - CodeForces Solution


brute force implementation *1200

Please click on ads to support us..

Python Code:

from itertools import permutations
from typing import List

def initial_order_for_maximum_happiness(mat: List[List[int]]) -> int:
    def calculate_happiness(student: List[int]) -> int:
        hap = 0
        n  = 5
        while student:
            pair = [(student[i], student[i+1]) for i in range(0, n-1, 2)]
            for i, j in pair:
                hap += mat[i][j]
                hap += mat[j][i]
            n -= 1
            student = student[1:]
        return hap
    num = [0, 1, 2, 3, 4]
    possible = list(permutations(num))
    happiness = 0
    for order in possible:
        hap = calculate_happiness(order)
        happiness = max(happiness, hap)
    return happiness

a = []
for i in range(5):
    b = [int(j) for j in input().split()]
    a.append(b)
d = initial_order_for_maximum_happiness(a)
print(d)

C++ Code:

#include <bits/stdc++.h>

using namespace std;
int g[6][6];
int main()
{
    int n=5;
    for(int i=0;i<n;i++)
        for(int j=0; j<n; j++)
            cin>>g[i][j];
    int p[6], pans[6], ans=-1, tmp;
    for(int i=0; i<n; ++i)
        p[i]=i;
    
    do{
        tmp=g[p[0]][p[1]]+g[p[1]][p[0]];
        tmp+=g[p[2]][p[3]]+g[p[3]][p[2]];
        
        tmp+=g[p[1]][p[2]]+g[p[2]][p[1]];
        tmp+=g[p[3]][p[4]]+g[p[4]][p[3]];
        
        tmp+=g[p[2]][p[3]]+g[p[3]][p[2]];
        
        tmp+=g[p[3]][p[4]]+g[p[4]][p[3]];
        
        if(tmp>ans){
            ans=tmp;
            for(int i=0; i<n; ++i)
                pans[i]=p[i];
        }
    }
    while(next_permutation(p, p+n));
    cout<<ans<<endl;

    return 0;
}


Comments

Submit
0 Comments
More Questions

1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves